package com.leyou.item.web;


import com.github.pagehelper.PageInfo;
import com.leyou.item.pojo.Spu;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:yanjizhw@163.com
 * @date:2019-10-10 9:11
 * 描述:
 * @since:1.0.0
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ResponseBody
    @RequestMapping("queryGoods")
    public ResponseEntity<Map<String, Object>> queryGoods(@RequestParam(value = "key", required = false) String key,
                                                          @RequestParam(value = "saleable", required = false) String saleable,
                                                          @RequestParam(value="page", defaultValue = "1") Integer page){

        int pageSize = 10; //每页显示条数

        PageInfo<Spu> info = goodsService.queryGoods(key, saleable, page, pageSize);

        Map<String, Object> map = new HashMap<>();

        long totalPage = info.getTotal() / pageSize;

        if (info.getTotal() % pageSize != 0){
            totalPage ++;
        }

        // 封装数据，并返回
        map.put("page", page);
        map.put("pageSize", pageSize);
        map.put("totalPage", totalPage);
        map.put("list", info.getList());

        return ResponseEntity.ok(map);
    }
}
